package com.raspbian.monitor.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.raspbian.monitor.bean.SelectSensorDataReq;
import com.raspbian.monitor.entity.SensorData;
import com.raspbian.monitor.service.SensorDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/api/sensor")
public class SensorDataController {
    @Autowired
    SensorDataService sensorDataService;

    @PostMapping("/insertSensorData")
    public R insertSensorData(@RequestBody SensorData sensorData){
        sensorData.setCreateTime(new Date());
        R resp = sensorDataService.insertSensorData(sensorData);
        return resp;
    }

    @GetMapping("/selectLatestData")
    public R selectLatestData() {
        return sensorDataService.selectLatestData();
    }

    @GetMapping("/selectAppData")
    public R selectAppData(@RequestParam Long startTime) {
        return sensorDataService.selectAppData(new Date(startTime));
    }

    @GetMapping("/selectTodaySensorData")
    public R selectTodaySensorData() {
        return sensorDataService.selectTodaySensorData();
    }

    @PostMapping("/selectSensorDataTable")
    public R selectSensorDataTable(@RequestBody SelectSensorDataReq req) {
        return sensorDataService.selectSensorDataTable(req.getStartTime(), req.getEndTime(),
                req.getMinTemp(), req.getMaxTemp(),
                req.getMinHumidity(), req.getMaxHumidity(),
                req.getGasStatus());
    }

}
